/*
 * @Author: xiaosihan 
 * @Date: 2024-07-01 11:54:57 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-07-05 14:16:56
 */

import * as BABYLON from "@babylonjs/core";
import "@babylonjs/loaders/glTF";
import "./babylonLoader/DaeLoader/dae";

import Axis from "./component3d/Axis/Axis";
import ImportMesh from "./component3d/ImportMesh/ImportMesh";

console.log("BABYLON", window.BABYLON = BABYLON);

//创建babylon实例渲染对象
class HomeRender {

    constructor() {
        // 调用初始方法
        this.init();
    }

    size = new BABYLON.Vector2(100, 100);

    //创建画布
    canvas = document.createElement("canvas");

    //创建babylon引擎对象
    engine = new BABYLON.Engine(this.canvas, true); // Generate the BABYLON 3D engine

    // 创建场景
    scene = (() => {
        const scene = new BABYLON.Scene(this.engine);
        scene.useRightHandedSystem = true;
        return scene;
    })();

    // 创建相机
    camera = (() => {
        var camera = new BABYLON.ArcRotateCamera("ArcRotateCamera", -Math.PI / 2, Math.PI / 4, 20, BABYLON.Vector3.Zero(), this.scene);
        camera.inertia = 0.001;
        console.log("camera", camera);
        camera.setTarget(BABYLON.Vector3.Zero());
        camera.attachControl(this.canvas, true);
        return camera;
    })();

    //容器
    contaider?: HTMLDivElement | null;

    // 把画布加载到容器里面
    install(div?: HTMLDivElement | null) {
        if (div) {
            div.innerHTML = "";
            div.appendChild(this.canvas);
            // 安装完成后启动渲染
            this.engine.runRenderLoop(() => {
                this.setSize(div.clientWidth, div.clientHeight);
                this.scene.render();
            });
        }
    }

    // 修改尺寸
    setSize(width: number, height: number) {
        // 尺寸发生改变时调整尺寸
        if (this.size.x !== width || this.size.y !== height) {
            this.size.set(width, height);
            this.canvas.width = width;
            this.canvas.height = height;
            this.engine.resize();
        }
    }

    //初始方法
    init() {

    }

    //添加一个三维坐标线
    axis = (() => {
        const axis = new Axis(10, this.scene);
        return axis;
    })();

    // 添加一个半球光
    light = (() => {
        const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(0, 1, 0), this.scene);
        light.intensity = 0.7;
        return light;
    })();

    // 加载gltf
    // soldier = (() => {
    //     const soldier = new ImportMesh("./gltf/Soldier.gltf", this.scene);
    //     soldier.playAnimation("walk");//播放走路动画
    //     soldier.setPosition(1, 0, 0);
    //     return soldier;
    // })();

    //加载 dae
    stormtrooper = (() => {
        const soldier = new ImportMesh("./collada/stormtrooper.dae", this.scene);
        soldier.setPosition(-1, 0, 0);
        return soldier;
    })();

    //加载测试dae
    // stormtrooper = (() => {
    //     const soldier = new ImportMesh("./collada/test.dae", this.scene);
    //     soldier.setPosition(-2, 0, 0);
    //     return soldier;
    // })();

    // 添加一个地面
    ground = (() => {
        const ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 6, height: 6 }, this.scene);
        return ground;
    })();

}

// 创建一个单例对象
const homeRender = window.homeRender = new HomeRender();

export default homeRender;




